package com.shen.server;

import com.shen.pojo.MobileCard;
import com.shen.server.serive.CallService;
import com.shen.server.serive.NetSerice;
import com.shen.server.serive.SendService;
import com.shen.util.Common;

/**
 * Author:shen
 * CreateDate:2020/6/11.
 * Function:超人套餐
 */
public class SuperPackage extends ServicePackage implements CallService, NetSerice, SendService {
    private int talkTime=200;//通话时长
    private int smsCount=50;//短信条数
    private int flow=1024;//上网流量

    @Override
    public String showInfo() {
        return super.showInfo();
    }

    /**
     * 通话
     * @param minCount
     * @param mobileCard
     * @return
     */
    @Override
    public int call(int minCount, MobileCard mobileCard) throws Exception {
        int temp=minCount;
        //分两种方式
        for(int i=0;i<minCount;i++){
            //查看是否剩余1分钟
            if(this.talkTime-mobileCard.getRealTalkTime()>=1){
                //循环一次，加一
                mobileCard.setRealTalkTime(mobileCard.getRealTalkTime()+1);
            }else if(mobileCard.getMoney()>=0.2){//是否能支付
                //没有1分钟
                mobileCard.setRealTalkTime(mobileCard.getRealTalkTime()+1);
                //减余额 运行一次就减0.2
                mobileCard.setMoney(mobileCard.getMoney()-0.2);
                //计算消费
                mobileCard.setConsumAmount(mobileCard.getConsumAmount()+0.2);
            }else{
                temp=i;
                //不能支付，也没有
                throw new Exception("本次已通话"+i+"分钟，您的余额不足，请充值后再使用！\n");
            }
        }
        return temp;
    }

    @Override
    public int netPlay(int flow, MobileCard mobileCard) throws Exception {
        int temp=flow;
        //分为两种情况
        for (int i = 0; i <(flow*1024); i++) {
            //1.第一种套餐支持再消费1MB
            if(this.flow-mobileCard.getRealFlow()>=1){
                mobileCard.setRealFlow(mobileCard.getRealFlow()+1);
            }else if(mobileCard.getMoney()>=0.1){//是否能支付0.1元
                //2.第二种套餐不支持再消费1GB，需要支付额外的费用 超出的上网流量0.1元/MB
                mobileCard.setRealFlow(mobileCard.getRealFlow()+1);
                //计算额外费用
                mobileCard.setMoney(mobileCard.getMoney()-0.1);
                //消费
                mobileCard.setConsumAmount(mobileCard.getConsumAmount()+0.1);
            }else{
                temp=i;
                throw new Exception("本次已使用"+i+"MB,您的余额不足，请充值后再使用！\n");
            }
        }
        return temp;
    }

    @Override
    public int send(int count, MobileCard mobileCard) throws Exception {
        int temp=count;
        for (int i = 0; i <count; i++) {
            if(this.smsCount-mobileCard.getRealSMSCount()>=1){
                mobileCard.setRealSMSCount(mobileCard.getRealSMSCount()+1);
            }else if(mobileCard.getMoney()>=0.1){
                mobileCard.setRealSMSCount(mobileCard.getRealSMSCount()+1);
                mobileCard.setMoney(mobileCard.getMoney()-0.1);
                mobileCard.setConsumAmount(mobileCard.getRealTalkTime()+0.1);
            }else{
                temp=i;
                throw new Exception("本次发送"+i+"条短信,您的余额不足，请充值后再使用！\n");
            }
        }
        return temp;
    }

    public SuperPackage() {
        this.price=78;
    }

    public SuperPackage(int talkTime, int smsCount, int flow, double price) {
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
        this.price = price;
    }

    public int getTalkTime() {
        return talkTime;
    }

    public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
    }

    public int getSmsCount() {
        return smsCount;
    }

    public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "超人套餐：通话时长为"+this.talkTime+"分钟/月，短信条数为"+this.smsCount+"条/月，上网流量为"+ Common.dataFormat(this.flow*1.0/1024)+"GB/月";
    }


}
